﻿using Base.Api.Models;
using Base.Api.Service;
using Base.Data.Entity;
using Common.AspNetCore;
using Common.Infrastructure;
using Common.Infrastructure.Structs;
using Common.RunModels.Base;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Base.Api.Controllers
{
    [Route("api/BaseData")]
    public class BaseDataController : ApiController
    {
        private readonly BaseDataService authDataService;
        private readonly IConfiguration configuration;

        public BaseDataController(IServiceProvider provider, BaseDataService auth, IConfiguration config)
             : base(provider)
        {
            this.authDataService = auth;
            this.configuration = config;
        }

        [HttpPost, Route("GetDepartByCode")]
        public async Task<ApiActionResult<DepartModel>> GetDepartByCode([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<DepartModel>(async (param) =>
            {
                return await this.authDataService.GetDepartByCodeAsync(apiParam);
            });
        }

        [HttpPost, Route("GetUserNumByDepartCode")]
        public async Task<ApiActionResult<int>> GetUserNumByDepartCode([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<int>(async (param) =>
            {
                return await this.authDataService.GetUserNumByDepartCodeAsync(apiParam);
            });
        }

        [HttpPost, Route("GetOrgaliasCodeByOrg")]
        public async Task<ApiActionResult<string>> GetOrgaliasCodeByOrg([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<string>(async (param) =>
            {
                return await this.authDataService.GetOrgaliasCodeByOrgAsync(apiParam);
            });
        }

        [HttpPost, Route("GetDepartById")]
        public async Task<ApiActionResult<DepartModel>> GetDepartById([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<DepartModel>(async (param) =>
            {
                return await this.authDataService.GetDepartByIdAsync(apiParam);
            });
        }

        /// <summary>
        /// 获取部门上级
        /// </summary>
        /// <param name="apiParam"></param>
        /// <returns></returns>
        [HttpPost, Route("GetDepartParentById")]
        public async Task<ApiActionResult<DepartModel>> GetDepartParentById([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<DepartModel>(async (param) =>
            {
                return await this.authDataService.GetDepartParentByIdAsync(apiParam);
            });
        }

        [HttpPost, Route("GetMemberByCode")]
        public async Task<ApiActionResult<MemberModel>> GetMemberByCode([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<MemberModel>(async (param) =>
            {
                return await this.authDataService.GetMemberByCodeAsync(apiParam);
            });
        }

        [HttpPost, Route("GetMemberById")]
        public async Task<ApiActionResult<MemberModel>> GetMemberById([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<MemberModel>(async (param) =>
            {
                return await this.authDataService.GetMemberByIdAsync(apiParam);
            });
        }

        [HttpPost, Route("GetMemberByUserCode")]
        public async Task<ApiActionResult<MemberModel>> GetMemberByUserCode([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<MemberModel>(async (param) =>
            {
                return await this.authDataService.GetMemberByUserCodeAsync(apiParam);
            });
        }

        [HttpPost, Route("GetMemberByUserId")]
        public async Task<ApiActionResult<MemberModel>> GetMemberByUserId([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<MemberModel>(async (param) =>
            {
                return await this.authDataService.GetMemberByUserIdAsync(apiParam);
            });
        }

        [HttpPost, Route("GetOrgs")]
        public async Task<ApiActionResult<List<OrgCache>>> GetOrgs()
        {
            return await SafeExecuteAsync<List<OrgCache>>(async (param) =>
            {
                return await this.authDataService.GetOrgsAsync();
            });
        }

        /// <summary>
        /// 根据组织Id获取组织信息
        /// </summary>
        /// <param name="apiParam"></param>
        /// <returns></returns>
        [HttpPost, Route("GetOrgsById")]
        public async Task<ApiActionResult<OrgCache>> GetOrgsById([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetOrgsByIdAsync(apiParam.OrgId);
            });
        }

        [HttpPost, Route("GetUser")]
        public async Task<ApiActionResult<UserModel>> GetUser([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<UserModel>(async (param) =>
            {
                return await this.authDataService.GetUserAsync(apiParam);
            });
        }

        [HttpPost, Route("GetUsers")]
        public async Task<ApiActionResult<List<UserModel>>> GetUsers([FromBody] ApiParam apiParam) 
        {
            return await SafeExecuteAsync<List<UserModel>>(async (param) =>
            {
                return await this.authDataService.GetUsersAsync(apiParam);
            });
        }

        [HttpPost, Route("GetUserByCode")]
        public async Task<ApiActionResult<UserModel>> GetUserByCode([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<UserModel>(async (param) =>
            {
                return await this.authDataService.GetUserByCodeAsync(apiParam);
            });
        }

        [HttpPost, Route("GetUserByCodesOrNames")]
        public async Task<ApiActionResult<List<UserModel>>> GetUserByCodesOrNames([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<UserModel>>(async (param) =>
            {
                return await this.authDataService.GetUserByCodesOrNamesAsync(apiParam);
            });
        }

        [HttpPost, Route("GetUserFull")]
        public async Task<ApiActionResult<UserFullModel>> GetUserFull([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<UserFullModel>(async (param) =>
            {
                return await this.authDataService.GetUserFullAsync(apiParam);
            });
        }

        [HttpPost, Route("GetUserFullByCode")]
        public async Task<ApiActionResult<UserFullModel>> GetUserFullByCode([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<UserFullModel>(async (param) =>
            {
                return await this.authDataService.GetUserFullByCodeAsync(apiParam);
            });
        }

        [HttpPost, Route("GetUserFullByIds")]
        public async Task<ApiActionResult<List<UserFullModel>>> GetUserFullByIds([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<UserFullModel>>(async (param) =>
            {
                return await this.authDataService.GetUserFullByIdsAsync(apiParam);
            });
        }

        [HttpPost, Route("GetUserFullByOrg")]
        public async Task<ApiActionResult<List<UserModel>>> GetUserFullByOrg([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<UserModel>>(async (param) =>
            {
                return await this.authDataService.GetUserFullByOrgAsync(apiParam);
            });
        }

        [HttpPost, Route("GetSectionBandingLines")]
        public async Task<ApiActionResult<List<SectionBandingLine>>> GetSectionBandingLines([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetSectionBandingLinesAsync(apiParam);
            });
        }

        /// <summary>
        /// 获取用户负责的线体
        /// </summary>
        /// <param name="apiParam"></param>
        /// <returns></returns>
        [HttpPost, Route("GetLineModelByUserId")]
        public async Task<ApiActionResult<List<LineModel>>> GetLineModelByUserId([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetLineModelByUserIdAsync(apiParam);
            });
        }

        [HttpPost, Route("GetDepartmentModels")]
        public async Task<ApiActionResult<List<DepartmentModel>>> GetDepartmentModels([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetDepartmentModelsAsync(apiParam);
            });
        }

        [HttpPost, Route("GetLineChiefsUser")]
        public async Task<ApiActionResult<ApiPagedResult<t_user>>> GetLineChiefsUser([FromBody] ModelOrderPageRequest apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetLineChiefsUserAsync(apiParam);
            });
        }

        [HttpPost, Route("GetLineModels")]
        public async Task<ApiActionResult<List<LineModel>>> GetLineModels([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetLineModelsAsync(apiParam);
            });
        }

        [HttpPost, Route("GetTopDepartmentModels")]
        public async Task<ApiActionResult<ApiPagedResult<DepartModel>>> GetTopDepartmentModelsAsync([FromBody] ModelOrderPageRequest apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetTopDepartmentModelsAsync(apiParam);
            });
        }

        [HttpPost, Route("GetLineDepartmentModels")]
        public async Task<ApiActionResult<List<DepartModel>>> GetLineDepartmentModelsAsync([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetLineDepartmentModelsAsync(apiParam);
            });
        }

        [HttpPost, Route("GetDepartmentLevelModels")]
        public async Task<ApiActionResult<List<DepartModel>>> GetDepartmentLevelModels([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetDepartmentLevelModelsAsync(apiParam);
            });
        }

        [HttpPost, Route("GetCommonCache")]
        public async Task<ApiActionResult<CommonCache>> GetCommonCache()
        {
            return await SafeExecuteAsync<CommonCache>(async (param) =>
            {
                var r = await this.authDataService.GetOrgsAsync();
                CommonCache cache = new CommonCache();
                cache.OrgCaches.AddRange(r);
                cache.GroupUrl = this.configuration["GroupUrl"];
                return cache;
            });
        }

        [HttpPost, Route("GetDepartLineByDepartCode")]
        public async Task<ApiActionResult<DepartLineResultModel>> GetDepartLineByDepartCode([FromBody] DepartLineParamModel model)
        {
            return await SafeExecuteAsync(async (param) =>
                {
                    return await this.authDataService.GetDepartLineByDepartCodeAsync(model);
                });
        }

        [HttpPost, Route("GetDepartLineByUserByUserId")]
        public async Task<ApiActionResult<DepartLineByUserResultModel>> GetDepartLineByUserByUserId([FromBody] DepartLineByUserParamModel model)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetDepartLineByUserByUserIdAsync(model);
            });
        }

        [HttpPost, Route("GetUserLeaderByUserId")]
        public async Task<ApiActionResult<UserLeaderResultModel>> GetUserLeaderByUserId([FromBody] UserLeaderParamModel model)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.GetUserLeaderByUserIdAsync(model);
            });
        }

        [HttpPost, Route("GetUserDepartByUserId")]
        public async Task<ApiActionResult<UserDepartByUserIdResultModel>> GetUserDepartByUserId([FromBody] UserDepartByUserIdParamModel model)
        {
            return await SafeExecuteAsync(async (param) =>
                {
                    return await this.authDataService.GetUserDepartByUserIdAsync(model);
                });
        }

        [HttpPost, Route("GetAllDepartByOrgId")]
        public async Task<ApiActionResult<List<AllDepartModel>>> GetAllDepartByOrgId([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<AllDepartModel>>(async (param) =>
            {
                return await this.authDataService.GetAllDepartByOrgIdAsync(apiParam);
            });
        }

        [HttpPost, Route("GetDepartTree")]
        public async Task<ApiActionResult<DepartTreeResultModel>> GetDepartTree([FromBody] DepartTreeParamModel model)
        {
            return await SafeExecuteAsync<DepartTreeResultModel>(async (param) =>
            {
                return await this.authDataService.GetDepartTreeAsync(model);
            });
        }

        [HttpPost, Route("UpdateDepartLeaderUser")]
        public async Task<ApiActionResult<bool>> UpdateDepartLeaderUser([FromBody] UpdateDepartLeaderUserParamModel model)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.UpdateDepartLeaderUserAsync(model);
            });
        }

        /// <summary>
        /// 获取公告
        /// </summary>
        /// <param name="apiParam"></param>
        /// <returns></returns>
        [HttpPost, Route("GetNotices")]
        public async Task<ApiActionResult<List<t_notice>>> GetNotices([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<t_notice>>(async (param) =>
            {
                return await this.authDataService.GetNoticesAsync(apiParam);
            });
        }

        /// <summary>
        /// 分页获取公告
        /// </summary>
        /// <param name="modelRequest"></param>
        /// <returns></returns>
        [HttpPost, Route("GetNoticesPageList")]
        public async Task<ApiActionResult<ApiPagedResult<NoticeModel>>> GetNoticesPageList([FromBody] ModelOrderPageRequest modelRequest)
        {
            return await SafeExecuteAsync<ApiPagedResult<NoticeModel>>(async (param) =>
            {
                return await authDataService.GetNoticesAsync(modelRequest);
            });
        }

        /// <summary>
        /// 保存公告
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost, Route("SaveNotice")]
        public async Task<ApiActionResult<bool>> SaveNotice([FromBody] ModelSaveRequest request)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await authDataService.SaveNotice(request);
            });
        }

        /// <summary>
        /// 获取区域
        /// </summary>
        /// <param name="apiParam"></param>
        /// <returns></returns>
        [HttpPost, Route("GetIQCAreaModel")]
        public async Task<ApiActionResult<IQCAreaModel>> GetIQCAreaModel([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<IQCAreaModel>(async (param) =>
            {
                return await this.authDataService.GetIQCAreaModelAsync(apiParam);
            });
        }

        /// <summary>
        /// 获取区域--根据编号
        /// </summary>
        /// <param name="apiParam"></param>
        /// <returns></returns>
        [HttpPost, Route("GetIQCAreaModelByCode")]
        public async Task<ApiActionResult<IQCAreaModel>> GetIQCAreaModelByCode([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<IQCAreaModel>(async (param) =>
            {
                return await this.authDataService.GetIQCAreaModelByCodeAsync(apiParam);
            });
        }

        /// 获取证书
        /// </summary>
        /// <param name="apiParam"></param>
        /// <returns></returns>
        [HttpPost, Route("GetCertificates")]
        public async Task<ApiActionResult<List<UserCertificateModel>>> GetCertificates([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<UserCertificateModel>>(async (param) =>
            {
                return await this.authDataService.GetCertificatesAsync(apiParam);
            });
        }

        /// <summary>
        /// 获取部门信息集合
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("GetDeparts")]
        public async Task<ApiActionResult<List<DepartModel>>> GetDeparts([FromBody] DepartsModel model)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await authDataService.GetDepartsAsync(model);
            });
        }

        /// <summary>
        /// 根据用户ID获取所属部门下的用户集合
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("GetUsersByUserId")]
        public async Task<ApiActionResult<List<DepartUserResultModel>>> GetUsersByUserId([FromBody] UsersByUserIdModel model)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                LibCheckNullUtils.CheckNull(model, "model");
                return await authDataService.GetUsersByUserIdAsync(model);
            });
        }

        /// <summary>
        /// 根据用户ID和层级向上获取部门信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("GetDepartByUserIdAndLevel")]
        public async Task<ApiActionResult<DepartModel>> GetDepartByUserIdAndLevel([FromBody] DepartByUserIdAndLevelModel model)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                LibCheckNullUtils.CheckNull(model, "model");
                return await authDataService.GetDepartByUserIdAndLevelAsync(model);
            });
        }

        /// <summary>
        /// 根据员工ID获取新员工数量
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("GetNewUserCount")]
        public async Task<ApiActionResult<int>> GetNewUserCount([FromBody] ApiParam model)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                LibCheckNullUtils.CheckNull(model, "model");
                LibCheckNullUtils.CheckNull(model.Keyword, "model.Keyword");
                var ids = model.Keyword.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
                return await authDataService.GetNewUserCountAsync(ids);
            });
        }
    }
}
